package com.shrct.businessprocess.product.impl;

import com.shrct.businessprocess.product.RCCompanyService;
import com.shrct.businessprocess.product.RCCompanyServiceEntry;
import com.shrct.businessprocess.product.RCLogicProduct;
import com.shrct.businessprocess.product.RCRegisterCompanyServiceEvent;
import com.shrct.businessprocess.product.impl.RCCompanyServiceEntryImpl;
import com.shrct.businessprocess.product.impl.RCLogicProductImpl;
import com.shrct.businessprocess.product.impl.RCRegisterCompanyServiceEventImpl;
import com.shrct.businessprocess.product.proxy.RCCompanyServiceProxy;
import edu.fudan.langlab.domain.common.DateRange;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCCOMPANYSERVICE_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rccompanyservice")
public class RCCompanyServiceImpl extends BaseModelObject implements RCCompanyService {
  public RCCompanyServiceImpl() {
    super();
  }
  
  public RCCompanyServiceImpl(final RCLogicProduct logicProduct) {
    super();
    this.logicProduct = logicProduct;
  }
  
  private String name;
  
  public String getName() {
    return this.name;
  }
  
  public RCCompanyService setName(final String name) {
    this.name = name;
    return this;			
    
  }
  
  @JoinColumn(name = "logicProduct_id")
  @ManyToOne(targetEntity = RCLogicProductImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCLogicProduct logicProduct;
  
  public RCLogicProduct getLogicProduct() {
    return this.logicProduct;
  }
  
  public RCCompanyService setLogicProduct(final RCLogicProduct logicProduct) {
    this.logicProduct = logicProduct;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCCompanyServiceEntryImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "companyService")
  private Collection<RCCompanyServiceEntry> entries = new java.util.ArrayList<com.shrct.businessprocess.product.RCCompanyServiceEntry>();;
  
  public Collection<RCCompanyServiceEntry> getEntries() {
    return this.entries;
  }
  
  public RCCompanyServiceEntry createAndAddtoEntries(final String projectid) {
    getEntries().size();
    com.shrct.businessprocess.product.RCCompanyServiceEntry rCCompanyServiceEntry = new com.shrct.businessprocess.product.impl.RCCompanyServiceEntryImpl(projectid,this);
    getObjectFactory().create(rCCompanyServiceEntry);
    getEntries().add(rCCompanyServiceEntry);
    return rCCompanyServiceEntry;
    
  }
  
  public RCCompanyServiceEntry createAndAddtoEntries(final String projectid, final Procedure1<RCCompanyServiceEntry> updater) {
    getEntries().size();
    com.shrct.businessprocess.product.RCCompanyServiceEntry rCCompanyServiceEntry = new com.shrct.businessprocess.product.impl.RCCompanyServiceEntryImpl(projectid,this);
    getObjectFactory().create(rCCompanyServiceEntry);
    getEntries().add(rCCompanyServiceEntry);
    return rCCompanyServiceEntry;
    
  }
  
  public RCCompanyService removeAllEntries() {
    for(RCCompanyServiceEntry obj : this.entries){
    	getObjectFactory().delete(obj);
    }
    this.entries.clear();
    return this;	
    
  }
  
  public RCCompanyService removeFromEntries(final RCCompanyServiceEntry rCCompanyServiceEntry) {
    this.entries.remove(rCCompanyServiceEntry);
    getObjectFactory().delete(rCCompanyServiceEntry);
    return this;
    
  }
  
  public RCCompanyServiceEntry registerCompanyServiceEntry(final String projectid, final Date startDate, final Date dueDate) {
    RCCompanyServiceEntry _xblockexpression = null;
    {
      DateRange _dateRange = new DateRange();
      final Procedure1<DateRange> _function = new Procedure1<DateRange>() {
          public void apply(final DateRange it) {
            it.setStartDate(startDate);
            it.setFinishDate(dueDate);
          }
        };
      final DateRange newRange = ObjectExtensions.<DateRange>operator_doubleArrow(_dateRange, _function);
      RCCompanyServiceEntry _createAndAddtoEntries = this.createAndAddtoEntries(projectid);
      final Procedure1<RCCompanyServiceEntry> _function_1 = new Procedure1<RCCompanyServiceEntry>() {
          public void apply(final RCCompanyServiceEntry it) {
            it.setStart(startDate);
            it.setEnd(dueDate);
            RCCompanyServiceImpl.this.logicProduct.updateCompanyServiceStatus();RCRegisterCompanyServiceEvent rCRegisterCompanyServiceEvent=RCRegisterCompanyServiceEventImpl.create(RCCompanyServiceImpl.this.logicProduct,it,getObjectFactory());
            final Procedure1<RCRegisterCompanyServiceEvent> _function = new Procedure1<RCRegisterCompanyServiceEvent>() {
                public void apply(final RCRegisterCompanyServiceEvent it) {
                  Date _day = DateUtil.today();
                  it.setDate(_day);
                  BeanFactory _beanFactory = RCCompanyServiceImpl.this.getBeanFactory();
                  ISystemUserService _bean = _beanFactory.<ISystemUserService>getBean(ISystemUserService.class);
                  User _currentUser = _bean.getCurrentUser();
                  it.setOperator(_currentUser);
                }
              };
            ObjectExtensions.<RCRegisterCompanyServiceEvent>operator_doubleArrow(rCRegisterCompanyServiceEvent, _function);
          }
        };
      RCCompanyServiceEntry _doubleArrow = ObjectExtensions.<RCCompanyServiceEntry>operator_doubleArrow(_createAndAddtoEntries, _function_1);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
  
  public void unregisterCompanyServiceEntry(final RCCompanyServiceEntry companyServiceEntry) {
    IGenericQuery<RCRegisterCompanyServiceEvent> query=getObjectFactory().createGenericQuery(RCRegisterCompanyServiceEvent.class,"select e from com.shrct.businessprocess.product.impl.RCRegisterCompanyServiceEventImpl as e   where ((e.companyService) = (:companyServiceEntry)) and  e.active = 1      ").setParameter("companyServiceEntry",companyServiceEntry);
    RCRegisterCompanyServiceEvent _uniqueResult = query.uniqueResult();getObjectFactory().delete(_uniqueResult);
    
    RCCompanyService _companyService = companyServiceEntry.getCompanyService();
    _companyService.removeFromEntries(companyServiceEntry);
    RCCompanyService _companyService_1 = companyServiceEntry.getCompanyService();
    RCLogicProduct _logicProduct = _companyService_1.getLogicProduct();
    _logicProduct.updateCompanyServiceStatus();
  }
  
  public static RCCompanyService create(final RCLogicProduct logicProduct, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.product.RCCompanyService rCCompanyService = new com.shrct.businessprocess.product.impl.RCCompanyServiceImpl(
    	logicProduct
    );
    objectFactory.create(rCCompanyService);
    return rCCompanyService;			
    
  }
  
  public static RCCompanyService createRCCompanyServiceImpl(final RCLogicProduct logicProduct, final IObjectFactory objectFactory, final Procedure1<RCCompanyService> init) {
    com.shrct.businessprocess.product.RCCompanyService rCCompanyService = new com.shrct.businessprocess.product.impl.RCCompanyServiceImpl(
    	logicProduct
    );
    objectFactory.create(rCCompanyService);
    init.apply(rCCompanyService);
    
    return rCCompanyService;			
    
  }
  
  public RCCompanyServiceProxy toProxy() {
    com.shrct.businessprocess.product.proxy.RCCompanyServiceProxy proxy = new com.shrct.businessprocess.product.proxy.RCCompanyServiceProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
